package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcOldmanSanitationRecordMapper;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcOldmanSanitationRecord;
import com.csun.cmny.provider.model.domain.OmcSanitationService;
import com.csun.cmny.provider.model.domain.OmcSanitationServiceBind;
import com.csun.cmny.provider.model.dto.sanitation.SanitationRecordSaveDto;
import com.csun.cmny.provider.model.dto.sanitation.SanitationRecordSelectDto;
import com.csun.cmny.provider.model.dto.sanitation.SanitationRecordUpdateDto;
import com.csun.cmny.provider.model.vo.sanitation.HuaLongSanitationVo;
import com.csun.cmny.provider.model.vo.sanitation.SanitationRecordVo;
import com.csun.cmny.provider.pojo.Oldman;
import com.csun.cmny.provider.service.OldmanService;
import com.csun.cmny.provider.service.OmcOldmanSanitationRecordService;
import com.csun.cmny.provider.service.OmcSanitationServiceBindService;
import com.csun.cmny.provider.service.OmcSanitationServiceService;
import com.csun.cmny.util.PublicUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.LinkedList;
import java.util.List;

@Slf4j
@Service
@Transactional
public class OmcOldmanSanitationRecordServiceImpl extends BaseService<OmcOldmanSanitationRecord> implements OmcOldmanSanitationRecordService {

    @Resource
    private OldmanService oldmanService;

    @Resource
    private OmcSanitationServiceBindService bindService;

    @Resource
    private OmcSanitationServiceService serviceService;

    @Resource
    private OmcOldmanSanitationRecordMapper sanitationRecordMapper;

    @Override
    public int addSanitationRecord(SanitationRecordSaveDto sanitationRecordSaveDto, LoginAuthDto loginAuthDto) {

        //校验本日期是否已经有卫生信息记录
        SanitationRecordSelectDto sanitationRecordSelectDto = new SanitationRecordSelectDto();
        sanitationRecordSelectDto.setOldManId(sanitationRecordSaveDto.getOldManId());
        sanitationRecordSelectDto.setRecordDate(sanitationRecordSaveDto.getRecordDate());

        List<SanitationRecordVo> list = selectSanitationRecord(sanitationRecordSelectDto);

        if (PublicUtil.isNotEmpty(list)){
            throw new OmcBizException(ErrorCodeEnum.OMC10032038);
        }

        //新增老人卫生信息记录
        OmcOldmanSanitationRecord omcOldmanSanitationRecord = new OmcOldmanSanitationRecord();
        BeanUtils.copyProperties(sanitationRecordSaveDto,omcOldmanSanitationRecord);
        omcOldmanSanitationRecord.setGroupId(loginAuthDto.getGroupId());
        omcOldmanSanitationRecord.setBuildingNo(sanitationRecordSaveDto.getBudingNo());

        int save = save(omcOldmanSanitationRecord);


        //新增与卫生服务的绑定关系
        LinkedList<OmcSanitationServiceBind> binds = new LinkedList<>();
        for (Long serviceId : sanitationRecordSaveDto.getServiceIds()) {
            OmcSanitationService omcSanitationService = serviceService.selectByKey(serviceId);

            if (PublicUtil.isEmpty(omcSanitationService)){
                throw new OmcBizException(ErrorCodeEnum.OMC10032037,serviceId);
            }

            OmcSanitationServiceBind serviceBind = new OmcSanitationServiceBind();
            serviceBind.setRecordId(omcOldmanSanitationRecord.getId());
            serviceBind.setServiceId(serviceId);
            serviceBind.setServiceName(omcSanitationService.getServiceName());

            binds.add(serviceBind);
        }

        int bind=bindService.saveBinds(binds);


        return save;
    }

    @Override
    public List<SanitationRecordVo> selectSanitationRecord(SanitationRecordSelectDto sanitationRecordSaveDto) {

        List<SanitationRecordVo> list=sanitationRecordMapper.selectSanitationRecord(sanitationRecordSaveDto);

        for (SanitationRecordVo sanitationRecordVo : list) {
            OmcSanitationServiceBind serviceBind = new OmcSanitationServiceBind();
            serviceBind.setRecordId(sanitationRecordVo.getId());

            List<OmcSanitationServiceBind> select = bindService.select(serviceBind);
            sanitationRecordVo.setSanitationServices(select);

        }

        return list;
    }

    @Override
    public int updateSanitationRecord(SanitationRecordUpdateDto sanitationRecordUpdateDto) {

        int update=0;

        //如果有备注更改,修改原数据
        if (PublicUtil.isNotEmpty(sanitationRecordUpdateDto.getRemark())){
            OmcOldmanSanitationRecord omcOldmanSanitationRecord = new OmcOldmanSanitationRecord();
            BeanUtils.copyProperties(sanitationRecordUpdateDto,omcOldmanSanitationRecord);
             update = update(omcOldmanSanitationRecord);
        }

        //删除原有绑定
        OmcSanitationServiceBind omcSanitationServiceBind = new OmcSanitationServiceBind();
        omcSanitationServiceBind.setRecordId(sanitationRecordUpdateDto.getId());
        bindService.delete(omcSanitationServiceBind);

        //新增与卫生服务的绑定关系
        LinkedList<OmcSanitationServiceBind> binds = new LinkedList<>();
        for (Long serviceId : sanitationRecordUpdateDto.getServiceIds()) {
            OmcSanitationService omcSanitationService = serviceService.selectByKey(serviceId);

            if (PublicUtil.isEmpty(omcSanitationService)){
                throw new OmcBizException(ErrorCodeEnum.OMC10032037,serviceId);
            }

            OmcSanitationServiceBind serviceBind = new OmcSanitationServiceBind();
            serviceBind.setRecordId(sanitationRecordUpdateDto.getId());
            serviceBind.setServiceId(serviceId);
            serviceBind.setServiceName(omcSanitationService.getServiceName());

            binds.add(serviceBind);
        }
        int bind=bindService.saveBinds(binds);
        return 0;
    }

    /**
     * 根据老人Id和月份查询个人卫生记录
     *
     * @param elderId 老人Id
     * @param month   月份(yyyy-MM)
     *
     * @return the list.
     */
    @Override
    public List<HuaLongSanitationVo> queryElderAndMonth(Integer elderId, String month) {

        return sanitationRecordMapper.queryElderAndMonth(elderId, month);
    }
}
